package com.neuedu.ssm;

import com.alibaba.druid.pool.DruidDataSource;
import com.neuedu.ssm.annotation.MyAnnotation;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;

/**
 * @author 金山
 * 项目：springmvc-learn
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2025/3/198:44
 */
@Configuration
@ComponentScan(basePackages = "com.neuedu.ssm")
@MapperScan("com.neuedu.ssm.mapper")
@EnableTransactionManagement
@PropertySource("classpath:jdbc.properties")
public class SSMConfigAPP {


    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.driverClass}")
    private String driverClass;


    /**
     * 声明数据源
     * @return
     */
    // @Bean
    // public DataSource dataSource(){
    //     DruidDataSource dataSource = new DruidDataSource();
    //     dataSource.setUrl("jdbc:mysql://localhost:3306/his?useSSL=false");
    //     dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    //     dataSource.setUsername("root");
    //     dataSource.setPassword("root");
    //
    //
    //     return dataSource;
    // }

    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverClass);
        dataSource.setUsername(username);
        dataSource.setPassword(password);


        return dataSource;
    }


    /**
     * 事务管理器(  spring-tx包  已经写好的 通知)
     * @param dataSource
     * @return
     */
    @Bean
    public TransactionManager  transactionManager(DataSource dataSource){

       TransactionManager transactionManager  = new DataSourceTransactionManager(dataSource);
       return  transactionManager;
    }



    /**
     * SqlSessionFactory
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource ) throws Exception {

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        //设置 mybaits的 settingxxxxxx
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setLogImpl(StdOutImpl.class);
        sqlSessionFactoryBean.setConfiguration(configuration);

        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));


        return sqlSessionFactoryBean.getObject();

    }

    // /**
    //  * 扫描 MyBatis中 Mapper的 包，并注册在 IOC容器中
    //  * @param sqlSessionFactory
    //  * @return
    //  */
    // @Bean
    // public MapperScannerConfigurer mapperSanccer(SqlSessionFactory sqlSessionFactory){
    //     MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
    //
    //
    //     //创建一堆 Mapper的扫描
    //
    //     scannerConfigurer.setBasePackage("com.neuedu.ssm.mapper");
    //     scannerConfigurer.setSqlSessionFactory(sqlSessionFactory);
    //
    //     return  scannerConfigurer;
    // }





}